/**
 * 文件名: HttpGrpcDemo.cc
 * 平台: Linux
 * 作者: Blxt
 * 描述:
 *  - curl测试Demo,实现了3个Http接口请求
 *     - https://www.baidu.com 的Get接口测试
 *     - http://192.168.51.201:8505/webtelnet/api/test/createDir 的Post接口测试
 *     - http://192.168.51.201:8505/webtelnet/api/test/getTest 的Get接口测试
 *  - Grpc测试demo
 *
 * 时间: 20230509 0949
 */
#include <iostream>
#include <memory>
#include <string>

#include <stdio.h>
#include <curl/curl.h>
#include "./src/curlhelp/curlHelper.h"
#include "src/grpcTest/gRpcHelper.h"


// 回调函数,用于处理 接口返回数据信息
size_t HttpsReturnMsg1(void* buffer, size_t size, size_t nmemb);
size_t HttpsReturnMsg2(void* buffer, size_t size, size_t nmemb);
size_t HttpsReturnMsg3(void* buffer, size_t size, size_t nmemb);


void httpTest(){
    // 消息头
    char heads[1024] = { 0 };
    snprintf(heads, 1024, "content-type: application/json");

    // 消息体
    char strMsgBody[1024] = { 0 };
    snprintf(strMsgBody, 1024, "{\r\n    \"path\":\"/测试1/测试2\",\r\n    \"name\": \"测试3\"\r\n}");

    // GET测试
    CURLcode ret = curlGetData((char*)"https://www.baidu.com", heads, strMsgBody, HttpsReturnMsg1);
    if (ret != CURLE_OK) {
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(ret));
    }
    // POST测试
    ret = curlPostData((char*)"http://192.168.51.201:8505/webtelnet/api/test/createDir", heads, strMsgBody, HttpsReturnMsg2);
    if (ret != CURLE_OK) {
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(ret));
    }
    // GET测试
    ret = curlGetData((char*)"http://192.168.51.201:8505/webtelnet/api/test/getTest?path=123&name=456", heads, strMsgBody, HttpsReturnMsg3);
    if (ret != CURLE_OK) {
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(ret));
    }
    // 全局清理
    curl_global_cleanup();
}

// 将请求返回的数据写在本地文件
size_t HttpsReturnMsg1(void* buffer, size_t size, size_t nmemb)
{
    char strMsgBody[1024] = { 0 };
    snprintf(strMsgBody, 1024, "%s", (char*)buffer);
    std::cout << "接口1测试:\r\n" << strMsgBody << "\r\n";

    FILE* fp = fopen("1.txt", "wb+");
    //fwrite(buffer, size, nmemb, fp);
    return size * nmemb;
}

size_t HttpsReturnMsg2(void* buffer, size_t size, size_t nmemb)
{
    char strMsgBody[1024] = { 0 };
    snprintf(strMsgBody, 1024, "%s", (char*)buffer);
    std::cout << "接口2测试:\r\n" << strMsgBody << "\r\n";

    FILE* fp = fopen("1.txt", "wb+");
    //fwrite(buffer, size, nmemb, fp);
    return size * nmemb;
}

size_t HttpsReturnMsg3(void* buffer, size_t size, size_t nmemb)
{
    char strMsgBody[1024] = { 0 };
    snprintf(strMsgBody, 1024, "%s", (char*)buffer);
    std::cout << "接口3测试:\r\n" << strMsgBody << "\r\n";

    FILE* fp = fopen("1.txt", "wb+");
    //fwrite(buffer, size, nmemb, fp);
    return size * nmemb;
}


int main() {
    printf("*************httpTest Start*****************************\n");
    // Http测试
    httpTest();
    printf("******************************************\n");
    // grpc测试
    //grpcTest();
    app1Test();
    printf("**************grpcTest Start****************************\n");

    return 0;
}